package org.znxs.comfriend.utils;

import org.znxs.comfriend.models.Message;
import org.znxs.comfriend.models.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 登录用户
 */
import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

/**
 * 登录用户单例类
 */
public class LoginUser {

    private static final String PREF_NAME = "LoginUserPrefs";
    private static final String KEY_USER_JSON = "user_json";

    private static LoginUser instance;
    private User loginUser;

    private Context context;

    private LoginUser(Context context) {
        this.context = context.getApplicationContext();
        loadUserFromLocal(); // 从本地加载用户信息
    }

    public static synchronized LoginUser getInstance(Context context) {
        if (instance == null) {
            instance = new LoginUser(context);
        }
        return instance;
    }

    /**
     * 获取当前登录用户
     */
    public User getLoginUser() {
        return loginUser;
    }

    /**
     * 设置当前登录用户，并保存到本地
     */
    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
        saveUserToLocal(loginUser); // 保存用户信息到本地
    }

    /**
     * 清除登录用户信息
     */
    public void clearLoginUser() {
        this.loginUser = null;
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        preferences.edit().remove(KEY_USER_JSON).apply(); // 从本地清除用户信息
    }

    /**
     * 将用户信息保存到本地
     */
    private void saveUserToLocal(User user) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        // 使用 Gson 库将对象转换为 JSON 字符串
        String userJson = new Gson().toJson(user);
        editor.putString(KEY_USER_JSON, userJson);
        editor.apply();
    }

    /**
     * 从本地加载用户信息
     */
    private void loadUserFromLocal() {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String userJson = preferences.getString(KEY_USER_JSON, null);

        if (userJson != null) {
            // 使用 Gson 库将 JSON 字符串转换为对象
            this.loginUser = new Gson().fromJson(userJson, User.class);
        } else {
            this.loginUser = null; // 初始化为空用户
        }
    }

    /**
     * 添加消息到 messageHashMap
     *
     * @param senderId   发送方 ID
     * @param receiverId 接收方 ID
     * @param content    消息内容
     * @param timestamp  时间戳
     */
    public void addMessage(String receiverId, String senderId, String content, long timestamp) {
        Map<String, List<Message>> messageHashMap = loginUser.getMessageHashMap();
        if (!messageHashMap.containsKey(receiverId)) {
            messageHashMap.put(receiverId, new ArrayList<>());
        }
        messageHashMap.get(receiverId).add(new Message(senderId, content, timestamp));
    }

    public List<Message> getMessages(String receiverId) {
        Map<String, List<Message>> messageHashMap = loginUser.getMessageHashMap();
        return messageHashMap.getOrDefault(receiverId, new ArrayList<>());
    }

}
